<?php
declare(strict_types=1);

namespace App\Controller\Backend;

use App\Logic\AdminAnchorLogic;
use Exception;
use Hyperf\HttpServer\Annotation\AutoController;
use Psr\Http\Message\ResponseInterface;

#[AutoController(prefix: '/anchor', server: 'backend')]
class AnchorController extends AbstractController
{
    /**
     * 主播列表
     * @return ResponseInterface
     */
    public function lists(): ResponseInterface
    {
        $input = $this->request->all();
        $data = AdminAnchorLogic::anchorList($input);
        return $this->apiReturn(200, 'succ', $data);
    }

    /**
     * 主播详情
     * @return ResponseInterface
     */
    public function detail(): ResponseInterface
    {
        $userId = $this->request->input('userId');
        if (!$userId) {
            return $this->apiReturn(400, '参数错误');
        }
        $data = AdminAnchorLogic::anchorDetail($userId);
        return $this->apiReturn(200, 'succ', $data);
    }

    /**
     * 主播入驻申请列表
     * @return ResponseInterface
     */
    public function settledList(): ResponseInterface
    {
        $input = $this->request->all();
        $data = AdminAnchorLogic::anchorSettled($input);
        return $this->apiReturn(200, 'succ', $data);
    }

    /**
     * 主播入驻申请处理
     * @return ResponseInterface
     */
    public function auditSettled(): ResponseInterface
    {
        $id = (int)$this->request->input('id', 0);
        $input = $this->request->all();
        if (!$id || !in_array($input['status'], [1, 2])) {
            return $this->apiReturn(400, '参数错误');
        }
        if ($input['status'] == 1 && empty($input['assistantUserId'])) {
            return $this->apiReturn(400, '尚未给主播分配助理');
        }
        try {
            AdminAnchorLogic::anchorAuditSettled($id, $input);
            return $this->apiReturn(200, '操作成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 快速修改
     * @return ResponseInterface
     */
    public function updateField(): ResponseInterface
    {
        $userId = (int)$this->request->input('userid');
        $field = $this->request->input('field', '');
        $value = (int)$this->request->input('value', 0);
        if (!$userId || empty($field)) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            AdminAnchorLogic::updateField($userId, $field, $value);
            return $this->apiReturn(200, '修改成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }
}